<?php

class ClusterController extends CSupervisorController {
    private static $key = 'itbitbitb';

    public function actionIndex() {
        echo "OK\n";
    }

    public function actionGetLastPendingSubmission() {
        $submission = Submission::getFirstPending();

        if ($submission !== null && $_GET['key']===self::$key) {
            $retval = array();
            $retval['submission'] = $submission->getAttributes();
            $retval['problem'] = $submission->problem->getAttributes();
            $retval['problemtype'] = $submission->problem->problemtype->getAttributes();
            $submission->setGradeStatus(Submission::GRADE_STATUS_WAITING);
            $submission->save();

            echo json_encode($retval);
        }
        else
            echo "0";
    }
    
    public function actionGetProblems() {
    	if ($_GET['key'] === self::$key) {
            $problems = Problem::model()->findAll();
            $retval = array();
            foreach ($problems as $problem) {
                if ($problem->problemtype->name == "batchioi2010")
                    $retval[$problem->id] = $problem->token;
            }
            echo json_encode($retval);
    	}
    }

    public function actionGetProblem($id) {
        $problem = Problem::model()->findByPK($id);
        if ($problem !== null && $_GET['key'] === self::$key) {
            chdir(Yii::app()->params->config['evaluator']['problem']['problem_repository_path']);
            exec("zip -r " . Yii::app()->params->config['evaluator']['problem']['problem_repository_path'] . '/' .$problem->token.".zip " . $problem->token);
            $zipfile = Yii::app()->params->config['evaluator']['problem']['problem_repository_path'] . '/' .$problem->token.".zip";
            echo file_get_contents($zipfile);
            unlink($zipfile);
        }
    }
    
    public function actionUpdateSubmission($id) {
        $submission = Submission::model()->findByPK($id);
        if ($submission !== null && $_GET['key'] === self::$key) {
            print_r($_POST);

            $submission->grade_content = $_POST['grade_content'];
            $submission->grade_time = new CDbExpression("NOW()");
            $submission->grade_output = $_POST['grade_output'];
            $submission->grade_status = $_POST['grade_status'];
            $submission->verdict = $_POST['verdict'];
            $submission->score = $_POST['score'];
            $submission->afterFind();

            $submission->save(false);
            
            if ($submission->chapter_id != null) {
				$submission->chapter->updateUser($submission->submitter);            
            }
        }
    }

    public function actionCancelGrading($id) {
        $submission = Submission::model()->findByPK($id);
        if ($submission !== null && $_GET['key'] === self::$key) {
            $submission->setGradeStatus(Submission::GRADE_STATUS_PENDING);
            $submission->save();
        }
    }
    
    public function actionGetContestInformation($id) {
    	$contest = Contest::model()->findByPK($id);
    	if ($contest !== null && $_GET['key'] === self::$key) {
    		echo json_encode($contest->getAttributes());
    	}    	    
    }
    
}
